package com.ad2campaign.engine.core.bootstrap;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.ad2campaign.engine.core.handlers.JVMShutdownHandler;
import com.ad2campaign.engine.core.handlers.OPSHandler;
import com.ad2campaign.engine.utils.DatasourceUtil;
import com.ad2campaign.engine.utils.PortUtil;
import com.ad2campaign.engine.utils.PropertyLoader;
import com.ad2campaign.engine.web.jetty.EngineHttpServer;

public class StartUp {

	/**
	 * Entry point for the engine
	 * @param no command line argument required
	 * @throws Exception 
	 */
	public static  EngineHttpServer httpServer =null;
	public static void main(String[] args) throws Exception
	{

		Properties engineProperties=PropertyLoader.settings();
 		int restport= Integer.parseInt( engineProperties.getProperty("engine.restport", "9080"));
		int opsport= Integer.parseInt( engineProperties.getProperty("engine.opsport", "9009"));
		
		boolean isMaster = Boolean.parseBoolean(engineProperties.getProperty("isMasterNode"));
		
		if (!PortUtil.available(restport))
		{
			System.out.println("Rest operation port ["+restport+"] is not available. Please change it in conf/settings.conf key [engine.restport] value [available port]");
			System.exit(-1);
		}
		
		if (!PortUtil.available(opsport))
		{
			System.out.println("OPS port ["+opsport+"] is not available. Please change it in conf/settings.conf key [engine.opsport] value [available port]");
			System.exit(-1);
		}
		
 		
		
  		String serverLogPath= "./server/logs/"+engineProperties.getProperty("engine.accesslog",  "http.log");

  		//Info Banner Starts
  		System.out.println("");
  		System.out.println("");
  		System.out.println("");
		System.out.println("                  +---------------------------------------------------------------------------------+");
		System.out.println("                  |                               Generic Push Engine                               |");
		if (isMaster)
		{
		System.out.println("                  |  Engine will run on MASTER mode i.e all new jobs will scheduled from this node. |");
		}
		else
		{
		System.out.println("                  |      Engine will run on SLAVE mode i.e it will act as processor node.           |");
 		}
 		System.out.println("                  |                           Engine Version : 1.0.RELEASE                          |");
  		System.out.println("                  |                           Batch  Version : 2.2.0.RELEASE                        |");
 		System.out.println("                  |                           Spring Version : 3.2.2.RELEASE                        |");
 		System.out.println("                  |                        AD2CAMPAIGN All Rights Reserved 2014                     |");
		System.out.println("                  +---------------------------------------------------------------------------------+");
  		System.out.println("");
  		System.out.println("");
 		//Info Banner Starts
		
		
		//Wait for three seconds to make checked port up and listening.
		Thread.sleep(3000);

		//Check database connection to proceed.
 		if (!DatasourceUtil.checkDatasource())
		{
			System.out.println("You must have forgotten to provide mysql database connection setting at conf/settings.conf");
			System.out.println("Must set key [batch.jdbc.driver]   value [driver class of mysql]");
			System.out.println("Must set key [batch.jdbc.url]      value [ jdbc connecion URL  ]");
			System.out.println("Must set key [batch.jdbc.user]     value [   mysql user name   ]");
			System.out.println("Must set key [batch.jdbc.password] value [    mysql password   ]");
			System.exit(-1);
 		}
 		
		//Creating new instance of jetty server
  		httpServer = new EngineHttpServer(restport,serverLogPath);
  		
  		//Register shutdown hook for graceful shutdown
 	 	Runtime.getRuntime().addShutdownHook(new Thread(new JVMShutdownHandler(httpServer)));

 	 	//Start jetty at background
 	 	httpServer.start();

 	 	//Apache mina objects for handling shutdown command
 	 	IoAcceptor ioAcceptor =  new NioSocketAcceptor();
  		OPSHandler opsHandler = new OPSHandler(httpServer);
		ioAcceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
		ioAcceptor.setHandler(opsHandler);
		ioAcceptor.bind(new InetSocketAddress(opsport));
 
		
  
  	}

}
